package com.botao.controller;

import com.botao.pojo.Page;
import com.botao.pojo.R;
import com.botao.pojo.Student;
import com.botao.service.StudentService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Controller
public class StudentController {
    static Logger logger = Logger.getLogger(StudentController.class);
    @Autowired
    StudentService studentService;

    /**
     * 添加学生.do
     *
     * @param student
     * @param model
     * @return
     */
    @RequestMapping("addStudent")
    public String add(Student student, Model model) {
        if (studentService.add(student)) {
            model.addAttribute("msg", "添加成功");
        } else {
            model.addAttribute("msg", "添加失败");
        }
        return "addStudent";
    }

    /**
     * 学生分页
     *
     * @param currentPage
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("studentList/{currentPage}/{pageSize}")
    public String list(@PathVariable Long currentPage, @PathVariable Long pageSize, Model model) {
        Page<Student> studentPage = studentService.listStudent(currentPage, pageSize);
        model.addAttribute("studentPage", studentPage);
        return "studentList";
    }

    /**
     * 删除学生 异步Ajax
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteStudent")
    public R delete(String id) {
        studentService.delete(id);
        return R.success();
    }

    /**
     * 修改学生页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("modifyStudent/{id}")
    public String modifyStudent(@PathVariable String id, Model model) {
        Student student = studentService.getOne(id);
        model.addAttribute("student", student);
        return "modifyStudent";
    }

    /**
     * 修改学生
     *
     * @param student
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("modifyStudentUpdate")
    public String modifyStudentUpdate(Student student, HttpServletResponse response) throws IOException {
        logger.info(student);
        studentService.update(student);
        response.sendRedirect("studentList/1/10");
        return null;
    }

    /**
     * 搜索学生
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("searchStudent")
    public String searchStudent(String id, Model model) {
        Student student = studentService.searchStudent(id);
        model.addAttribute("student", student);
        logger.info(id);
        logger.info(student);
        return "searchStudentList";
    }

    @RequestMapping("studentListByClassId")
    public String studentListByClassId(int id,Model model) {
        List<Student> studentList = studentService.getStudentsByClassesId(id);
        model.addAttribute("studentList",studentList);
        return "studentListByClassId";
    }

    @ResponseBody
    @RequestMapping("studentExists")
    public R studentExists(String id) {
        return studentService.studentExists(id)?R.success():R.error();
    }

}
